<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" @click="handleCreate()"> 新增 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              // {
              //   // icon: 'clarity:add-text-line',
              //   label: '新增',
              //   onClick: handleCreate.bind(null, record),
              // },
              {
                // icon: 'clarity:note-edit-line',
                label: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                // icon: 'ant-design:delete-outlined',
                label: '删除',
                // color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import MenuDrawer from './MenuDrawer.vue';

  import { useDrawer } from '/@/components/Drawer';

  import { useMessage } from '/@/hooks/web/useMessage';

  import { columns, searchFormSchema, resetModelList } from './menu.data';
  import { deleteMenuApi, getMenuClientTree } from '/@/api/admin/menu';

  export default defineComponent({
    name: 'MenuManagement',
    components: { BasicTable, MenuDrawer, TableAction },
    setup() {
      const { createMessage } = useMessage();

      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '菜单列表',
        api: getMenuClientTree,
        columns,
        formConfig: {
          labelWidth: 75,
          schemas: searchFormSchema,
        },
        mobileInitParam: {
          menuType: 0,
          client: 1,
        },
        pagination: false,
        useSearchForm: true,
        showTableSetting: false,
        bordered: false,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          fixed: 'right',
        },
      });

      function handleCreate(record: Recordable = {}) {
        openDrawer(true, {
          record,
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        resetModelList();
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await deleteMenuApi(record.moduleId);
        createMessage.success('操作成功');
        reload();
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
